package cn.iocoder.yudao.module.call.controller.admin.Ivrgroup;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.Ivrgroup.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrgroup.IvrGroupDO;
import cn.iocoder.yudao.module.call.service.ivrgroup.IvrGroupService;

@Tag(name = "管理后台 - ivr组")
@RestController
@RequestMapping("/call/ivr-group")
@Validated
public class IvrGroupController {

    @Resource
    private IvrGroupService ivrGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建ivr组")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createIvrGroup(@Valid @RequestBody IvrGroupSaveReqVO createReqVO) {
        return success(ivrGroupService.createIvrGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ivr组")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:update')")
    public CommonResult<Boolean> updateIvrGroup(@Valid @RequestBody IvrGroupSaveReqVO updateReqVO) {
        ivrGroupService.updateIvrGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ivr组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:delete')")
    public CommonResult<Boolean> deleteIvrGroup(@RequestParam("id") Long id) {
        ivrGroupService.deleteIvrGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ivr组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<IvrGroupRespVO> getIvrGroup(@RequestParam("id") Long id) {
        IvrGroupDO ivrGroup = ivrGroupService.getIvrGroup(id);
        return success(BeanUtils.toBean(ivrGroup, IvrGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ivr组分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<PageResult<IvrGroupRespVO>> getIvrGroupPage(@Valid IvrGroupPageReqVO pageReqVO) {
        PageResult<IvrGroupDO> pageResult = ivrGroupService.getIvrGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrGroupRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得ivr组分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<List<IvrGroupRespVO>> getIvrGroupList() {
        IvrGroupPageReqVO pageReqVO = new IvrGroupPageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<IvrGroupDO> pageResult = ivrGroupService.getIvrGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult.getList(), IvrGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ivr组 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrGroupExcel(@Valid IvrGroupPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrGroupDO> list = ivrGroupService.getIvrGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ivr组.xls", "数据", IvrGroupRespVO.class,
                BeanUtils.toBean(list, IvrGroupRespVO.class));
    }

}